package io.wins.vertx.service.impl;

import io.vertx.core.Vertx;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.wins.vertx.entity.Purchase;
import io.wins.vertx.service.BaseService;
import io.wins.vertx.service.PurchaseService;

public class PurchaseServiceImpl extends BaseService implements PurchaseService {
    public PurchaseServiceImpl(final Vertx vertx) {
        this.vertx = vertx;
    }

    @Override
    public PurchaseService getPurchase(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_purchase where id = ? and is_delete = '0'")
            .put(PARAMS, new JsonArray().add(id));
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public PurchaseService delPurchase(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_purchase set is_delete = '1' where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.deleteSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public PurchaseService listPurchase(final String status, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select p.*, c.nickname from ds_purchase p " +
                "left join ds_customer c on p.customer_id = c.id " +
                "where p.is_delete = '0' order by p.updated_on desc");
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public PurchaseService listPurchaseByCust(final String customerId, final int status, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_purchase where customer_id = ? and is_delete = '0' order by created_on desc")
            .put(PARAMS, new JsonArray().add(customerId));
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public PurchaseService savePurchase(final Purchase p, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_purchase set detail=?, price=?, `count`=?, total=?, pay_type=?, area_name=?, address=?, status=?, updated_on=UNIX_TIMESTAMP() where id = ?)")
            .put(PARAMS, new JsonArray()
                .add(p.getDetail())
                .add(p.getPrice())
                .add(p.getCount())
                .add(p.getTotal())
                .add(p.getPayType())
                .add(p.getAreaName())
                .add(p.getAddress())
                .add(p.getStatus())
                .add(p.getId()));
        this.updateSql(sqlMes, this.resultUpdate(p.getId(), tMessage, "Error"));
        return this;
    }

    @Override
    public PurchaseService createPurchase(final Purchase p, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "insert into ds_purchase (id, sell_no, customer_id, item_id, item_type, title, detail, price, `count`, total, pay_type, area_name, address, status, created_on, updated_on) " +
                "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,UNIX_TIMESTAMP(),UNIX_TIMESTAMP())")
            .put(PARAMS, new JsonArray()
                .add(p.getId())
                .add(p.getSellNo())
                .add(p.getCustomerId())
                .add(p.getItemId())
                .add(p.getItemType())
                .add(p.getTitle())
                .add(p.getDetail())
                .add(p.getPrice())
                .add(p.getCount())
                .add(p.getTotal())
                .add(p.getPayType())
                .add(p.getAreaName())
                .add(p.getAddress())
                .add(p.getStatus()));
        this.insertSql(sqlMes, this.resultUpdate(p.getId(), tMessage, "Error"));
        return this;
    }

    @Override
    public PurchaseService updateStatus(final String id, final String status, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_purchase set status = ? where id = ?")
            .put(PARAMS, new JsonArray().add(status).add(id));
        this.updateSql(sqlMes, this.resultUpdate(id, tMessage, "Error"));
        return this;
    }
}
